//
//  TRMessageVeiw.m
//  WeiMiLan
//
//  Created by Mac on 15/10/27.
//  Copyright © 2015年 Mac. All rights reserved.
//

#import "TRMessageVeiw.h"

@interface TRMessageVeiw()
@property (strong, nonatomic) NSMutableArray *contents;

@end

@implementation TRMessageVeiw

- (void)awakeFromNib {
    [super awakeFromNib];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(listenComment:) name:@"kRefreshComment" object:nil];
}

- (void)listenComment: (NSNotification *)noti {
    [self loadData:1];
    
    if (ApplicationDelegate.messageArr.count == 0) {
        self.messageLabel.text = @"暂无消息";
    } else {
        NSDictionary *dict = ApplicationDelegate.messageArr.firstObject;
        
        self.messageLabel.text = dict[@"message"];
        
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyyMMddHHmmss"];
      //  NSDate *createDate = [formatter dateFromString:dict[@"time"]];
        
        
        NSString *strTime = [NSString stringWithFormat:@"%@",dict[@"time"]];
     //   strTime = [strTime substringFromIndex:5];
        
        self.timeLabel.text = strTime;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
}

- (void)loadData:(int)num
{
    NSDictionary *userInfo = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:QLIntToNumber(num) forKey:@"PAGE_NUM"];
    [dic ql_setInterge:[userInfo ql_intForKey:@"ID"] forKey:@"userId"];
    
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSString *strUrl = [NSString stringWithFormat:@"http://ping.vsmu.cn/app/Interface/Comment?type=%@",@"10006"];
    [requestManager GET:strUrl parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject[@"MESSAGE_INFO"][@"CODE"] isEqualToString:@"0000"]) {
            if (num==1) {
                //第一页
                if ([responseObject[@"COMMENT_RSP"][@"COMMENTS"] isKindOfClass:[NSDictionary class]]) {
                    [self.contents addObject:responseObject[@"COMMENT_RSP"][@"COMMENTS"]];
                }else{
                    self.contents = [NSMutableArray arrayWithArray:responseObject[@"COMMENT_RSP"][@"COMMENTS"]];
                }
            }else{
                [self.contents addObjectsFromArray:responseObject[@"COMMENT_RSP"][@"COMMENTS"]];
            }
            
            NSDictionary *commentDict = self.contents.firstObject;
            if (commentDict.count > 0) {
                
                self.myComment.text = [NSString stringWithFormat:@"%@ 评论: %@",commentDict[@"AUTHOR_NAME"],commentDict[@"CONTENT"]];
                
                
                
//                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//                [formatter setDateFormat:@"yyyyMMddHHmmss"];
                NSString *time = [NSString stringWithFormat:@"%@",commentDict[@"CREATE_DATE"]];
            //    NSDate *createDate = [formatter dateFromString:time];
                
            //    NSString *strTime = [NSString stringWithFormat:@"%@",[createDate ql_ToString]];
                time = [time substringFromIndex:5];
                
                
                self.myCommentTime.text = time;
 
            } else {
                self.myComment.text = @"暂无评论";
            }
  
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  
    }];
}

//"AUTHOR_HEAD_PIC" = "http://tp4.sinaimg.cn/3086786015/180/5727359582/1";
//"AUTHOR_ID" = 20173;
//"AUTHOR_NAME" = "\U4e0d\U53ef\U6570\U7684\U7231";
//CONTENT = 202020;
//"CREATE_DATE" = "2015-11-02 10:14:09";
//FLAG = old;
//ID = 623;
//"PRODUCT_DESC" = hehe;
//"PRODUCT_ID" = 75853;
//REPLIES = 0;

+ (instancetype)messageVeiw {
    return [[NSBundle mainBundle] loadNibNamed:@"TRMessageVeiw" owner:nil options:nil][0];
}
- (IBAction)remindCommentBtn:(UIButton *)sender {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(messageVeiwDidClickedBtnIndex:btnClickIndex:)]) {
        [self.delegate messageVeiwDidClickedBtnIndex:self btnClickIndex:sender.tag];
    }
    
}



@end
